package com.example.cinema.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cinema.bindingdata.VideoData;

import java.util.List;

/**
 * Recycler通用适配器
 * @param <T>
 */
public class CommentRecyAdapter<T> extends RecyclerView.Adapter<ItemVideHandler> {
    private List<T> data;
    private Context context;
    private int layoutid;
    private int Brid;

    public CommentRecyAdapter(List<T> data, Context context, int layoutid, int brid) {
        this.data = data;
        this.context = context;
        this.layoutid = layoutid;
        Brid = brid;
    }

    @NonNull
    @Override
    public ItemVideHandler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding vdb = DataBindingUtil.inflate(LayoutInflater.from(context),layoutid,parent,false);
        ItemVideHandler handler = new ItemVideHandler(vdb);
        return handler;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemVideHandler holder, int position) {
        holder.itembinding.setVariable(Brid,data.get(position));
        holder.itembinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(itemViewClick!=null)
                {
                    itemViewClick.ItemViewClick(v,position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public interface OnItemViewClick{
        void ItemViewClick(View view,int position);
    }
    private OnItemViewClick itemViewClick;
    public void setOnItemViewClick(OnItemViewClick viewClick)
    {
        itemViewClick = viewClick;
    }


}
class ItemVideHandler extends RecyclerView.ViewHolder{
    public ViewDataBinding itembinding;
    public ItemVideHandler(@NonNull ViewDataBinding itemView) {
        super(itemView.getRoot());
        itembinding = itemView;
    }

}

